package com.zhiyu.service.impl;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.zhiyu.service.RedisService;

@Service
public class RedisServiceImpl implements RedisService {
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Override
	public void setKey(String key, String value) {
		stringRedisTemplate.opsForValue().set(key, value);
	}

	@Override
	public void setKey(String key, String value, long expire) {
		setKey(key, value);
		expire(key, expire);
	}

	@Override
	public String getKey(String key) {
		return stringRedisTemplate.opsForValue().get(key);
	}

	@Override
	public boolean expire(String key, long expire) {
		return stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
	}

	@Override
	public void remove(String key) {
		stringRedisTemplate.delete(key);
	}

	@Override
	public Long increment(String key, long delta) {
		return stringRedisTemplate.opsForValue().increment(key, delta);
	}

	@Override
	public void addIntoSet(String key, String ...value) {
		stringRedisTemplate.opsForSet().add(key, value);
	}

	@Override
	public void removeFromSet(String key, String ...value) {
		stringRedisTemplate.opsForSet().remove(key, value);
	}

	@Override
	public long getSetSize(String key) {
		return stringRedisTemplate.opsForSet().size(key);
	}

	@Override
	public Set<String> getSetMembers(String key) {
		return stringRedisTemplate.opsForSet().members(key);
	}

	@Override
	public boolean isSetMember(String key, String target) {
		return stringRedisTemplate.opsForSet().isMember(key, target);
	}

}
